package io.mft.dev.bootstrap.adapter;

import java.util.Map;

import io.mft.api.domain.Api;
import io.mft.commons.config.ConfigProvider;
import io.mft.commons.ioc.MFTSpringRegistryProcessor;
import io.mft.commons.ioc.MFTSpringUtil;
import io.mft.datasource.ar.ARBootstrap;
import io.mft.datasource.cache.CacheBootstrap;
import io.mft.datasource.db.DbBootstrap;
import io.mft.dev.base.BasePluginApplication;
import io.mft.dev.base.init.MFTFrameworkInitor;
import io.mft.dev.base.init.MFTFrameworkInitor.MainHostProgramApplicationContextProvider;
import io.mft.plugin.PluginInvoker;
import io.mft.runtime.MFTApiExecutor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class MFTRuningAdapter<T>{
	public abstract void registerApi(Api api);
	public abstract void unregisterApi(Api api);
	public abstract T executeApi(String funcNo,Map<String, Object> parameter);
	
	public static void initConfig() {
		MFTFrameworkInitor.initHostProgramApplicationConfig();
		PluginInvoker.pluginId=ConfigProvider.get("plugin.id", "mainSys");
		MFTSpringRegistryProcessor.registerCommonBasePackages("baseMain-io.mft");
		MFTSpringRegistryProcessor.registerCommonScanPackages(PluginInvoker.pluginId+"-io.mft");
		MFTSpringRegistryProcessor.registerCommonBasePackages("baseMain-com.hxzq");
		MFTSpringRegistryProcessor.registerCommonScanPackages(PluginInvoker.pluginId+"-com.hxzq");
	}
	
	public final void start(MainHostProgramApplicationContextProvider provider) {
		log.warn("MFT初始化开始!");
		
		MFTFrameworkInitor.init(provider);
		
		MFTApiExecutor.getInstance().start();
		DbBootstrap.init(MFTApiExecutor.getInstance().getRuntimeConfig());
		CacheBootstrap.init(MFTApiExecutor.getInstance().getRuntimeConfig());
		ARBootstrap.init(MFTApiExecutor.getInstance().getRuntimeConfig());

		MFTApiExecutor.getInstance().getRuntimeConfig().getApis().forEach(api->{
			registerApi(api);
		});
		Map<String, BasePluginApplication> beanMap=MFTSpringUtil.getApplicationContext().getBeansOfType(BasePluginApplication.class);
		beanMap.values().forEach(bean->{
			try {
				bean.start();
			} catch (Exception e) {
				log.error("",e);
			}
		});
		log.warn("MFT初始化完毕!");
	}
	
}
